package com.qf.listener;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.constant.RabbitMQConstants;
import com.qf.model.StandardReport;
import com.qf.model.StandardSubmit;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 何星宇
 * @version V1.0
 * @Date 2021/11/25 16:17
 */

@Component
@Slf4j
public class Listener {
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Autowired
    private ObjectMapper objectMapper;

    @RabbitListener(queues = RabbitMQConstants.TOPIC_SMS_SEND_LOG)
    public void logQueue(Channel channel, StandardSubmit standardSubmit , Message message) throws IOException {
        //将日志数据存储到es服务器
        //构建Request对象
        IndexRequest request = new IndexRequest();
        request.index("sms_submit_log2021");
        request.id(standardSubmit.getSequenceId());
        //封装数据添加到Request对象
        String submit = objectMapper.writeValueAsString(standardSubmit);
        request.source(submit, XContentType.JSON);
        //将Request对象发送给ES，获取ES的响应
        IndexResponse resp = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        //获取result，判断是否是CREATED
        if ("CREATED".equalsIgnoreCase(resp.getResult().getLowercase())) {
            log.info("【搜索模块-添加数据到es】 添加数据成功！");
            //ack
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }else{
            log.info("【搜索模块-添加数据到es】 添加数据失败！");
        }
    }

    @RabbitListener(queues = RabbitMQConstants.TOPIC_UPDATE_SMS_REPORT)
    public void updateQueue(Channel channel, StandardReport standardReport , Message message) throws IOException {
        //根据短信的id, 到ES服务器中, 更改已经存储的短信日志数据中的发送状态
        //构建Request对象
        UpdateRequest request = new UpdateRequest();
        request.index("sms_submit_log2021");
        request.id(standardReport.getSequenceId());
        Map reportState = new HashMap();
        reportState.put("reportState",String.valueOf(standardReport.getState()));
        reportState.put("errorCode",standardReport.getErrorCode());
        request.doc(reportState);
        UpdateResponse resp = restHighLevelClient.update(request, RequestOptions.DEFAULT);
        //获取result，判断是否是CREATED///1463779309197070336
        if ("UPDATED".equalsIgnoreCase(resp.getResult().getLowercase())) {
            log.info("【搜索模块-修改数据到es】 修改数据成功！ 短信ID：{}",standardReport.getSequenceId());
            //ack
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }else{
            log.info("【搜索模块-修改数据到es】 修改数据失败！ 短信ID：{}",standardReport.getSequenceId());
        }
    }
}
